package com.xqh.study.springtxjms.web;

import com.xqh.study.springtxjms.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName CustomerResource
 * @Description jms transaction
 * @Author xuqianghui
 * @Date 2019/12/31 11:50
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/customer")
@Slf4j
public class CustomerResource {

    @Autowired
    private JmsTemplate jmsTemplate;

    @Autowired
    private CustomerService customerService;

    @RequestMapping(value = "msg/listen" )
    public void sendMsg(String msg){
        jmsTemplate.convertAndSend("customer:msg1:new", msg);
    }


    @RequestMapping(value = "msg/direct")
    public void directSendMsg(String msg){
        customerService.handle(msg);
    }

    @RequestMapping(value = "msg/list")
    public Object listMsg(){
        // 设置消息读取超时时间
        jmsTemplate.setReceiveTimeout(2000);
        return jmsTemplate.receiveAndConvert("customer:msg:reply");
    }

    @RequestMapping(value = "msg2/listen" )
    public void sendMsg2(String msg){
        jmsTemplate.convertAndSend("customer:msg2:new", msg);
    }


    @RequestMapping(value = "msg2/direct")
    public void directSendMsg2(String msg){
        customerService.handleInCode(msg);
    }
}
